package com.youtellv1.controller;

import com.github.pagehelper.PageInfo;
import com.youtellv1.config.ResultData;
import com.youtellv1.entity.Post;
import com.youtellv1.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/manager/posts")
public class PostController {
    @Autowired
    private PostService postService;

    @GetMapping("/list")
    public ResultData<PageInfo<Post>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "8") int size) {
        try {
            PageInfo<Post> pageInfo = postService.findPostByPage(page, size);
            return new ResultData<>(pageInfo);
        } catch (Exception e) {
            return new ResultData<>(500, "分页获取失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/all")
    public ResultData<List<Post>> getAllPosts() {
        try {
            List<Post> list = postService.findAll();
            return new ResultData<>(list);
        } catch (Exception e) {
            return new ResultData<>(500, "获取岗位列表失败：" + e.getMessage(), null);
        }
    }


    @GetMapping("/get/{pid}")
    public ResultData<Post> get(@PathVariable Integer pid) {
        try {
            if (pid == null) {
                return new ResultData<>(400, "岗位ID不能为空", null);
            }
            Post post = new Post();
            post.setPid(pid);
            Post result = postService.findAllService(post);
            if (result == null) {
                return new ResultData<>(404, "未找到岗位信息", null);
            }
            return new ResultData<>(result);
        } catch (Exception e) {
            return new ResultData<>(500, "获取岗位信息失败 : " + e.getMessage(), null);
        }
    }

    @PostMapping("/save")
    public ResultData<Void> save(
            @RequestParam("pname") String pname,
            @RequestParam("post_sal") String postSal
    ) {
        try {
            Post post = new Post();
            post.setPname(pname);
            post.setPost_sal(Float.valueOf(postSal));
            postService.saveService(post);
            return new ResultData<>(200, "添加成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "保存岗位信息失败 : " + e.getMessage(), null);
        }
    }

    @PostMapping("/update")
    public ResultData<Void> updatePost(@RequestBody Post post) {
        try {
            postService.updateService(post);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "更新失败: " + e.getMessage(), null);
        }
    }

    @DeleteMapping("/delete/{pid}")
    public ResultData<Void> delete(@PathVariable Integer pid) {
        try {
            Post post = new Post();
            post.setPid(pid);
            postService.deleteService(post);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "删除岗位信息失败 : " + e.getMessage(), null);
        }
    }
}
